% function create_data_struct_csv_profiler_Nortek.m
%
%Syntax: Ndata=create_data_struct_csv_profiler_Nortek(cruise,folder,distance,filename)
%
%Matlab script that compiles all burst average Nortek data from one curise into
%one structure where each row corresponds to a different sample/depth. 
%
%Saves the data into a csv summary file (which can be opened as an excel spreadsheet).
%
%Inputs
%   Assigned in getNortek_burstAVGdata_fromprofiler
%       cruise: Cruise Name
%       folder:folder containing Nortek files and location for file output
%       distance: vector with distance from the York River Mouth (or reference point of your choice).
%       filename:vector containing the names of Nortek files to compile
%
%Output
%   Cruise_Nortek.csv:csv file with compiles Nortek data
%   Ndata:structure with compiled Nortek data. (See end of code for variable
%   name descriptions).
%
%KAF September 2016
%
function Ndata=create_data_struct_csv_profiler_Nortek(cruise,folder,distance,filename, backConc)

files=dir([folder '*_AVG.mat']); %Top Middle and Bottom
%files=dir('*_AVG.mat');
%files=dir([folder '*AVG_adjust_for_backConc.mat']);

%% Load all avg.mat files into 1 matrix

station=char(filename);

for ii=1:length(files)
  nortek(ii)=(cat(2,(load(fullfile(folder,files(ii).name),'datout'))));
  bscatter(ii)=[load(fullfile(folder,files(ii).name),'bscatter_stats')];
end

for ii=1:length(files)
Ndata.bscatter_stats(ii).ampmn= bscatter(ii).bscatter_stats.ampmn;
Ndata.bscatter_stats(ii).std_ampmn= bscatter(ii).bscatter_stats.std_ampmn;
Ndata.bscatter_stats(ii).stderr_ampmn= bscatter(ii).bscatter_stats.stderr_ampmn;
Ndata.bscatter_stats(ii).nsmp_good= bscatter(ii).bscatter_stats.nsmp_good;
Ndata.bscatter_stats(ii).nsmp= bscatter(ii).bscatter_stats.nsmp;
end

NORTEK=[nortek(1).datout];
id=str2num(station(1,2:end))*ones(size(nortek(1).datout,1),1);
dist=distance(1)*ones(size(nortek(1).datout,1),1);

for jj=2:length(nortek)
NORTEK=[NORTEK
    nortek(jj).datout];

id=[id
    str2num(station(jj,2:end))*ones(size(nortek(jj).datout,1),1)];

dist=[dist
    distance(jj)*ones(size(nortek(jj).datout,1),1)];
end

C_pump=backConc*ones(size(NORTEK,1),1);


%% Make Headers
%For Data File

hdr1_data=[cellstr('StationID') cellstr('Distance') cellstr('Pressure') cellstr('u') cellstr('v') cellstr('w') cellstr('std_u') cellstr('std_v') cellstr('std_w')... 
    cellstr('iu') cellstr('iv') cellstr('iw') cellstr('pg_u') cellstr('pg_v') cellstr('pg_w') cellstr('upr2') cellstr('uvpr') cellstr('uwpr') cellstr('vpr2') cellstr('vwpr') cellstr('wpr2')...
    cellstr('heading') cellstr('pitch') cellstr('roll') cellstr('temp') cellstr('compass')...
    cellstr('b1') cellstr('b2')  cellstr('C') cellstr('C_min_pump') cellstr('C_min_burst')...
    cellstr('Cupr') cellstr('Cvpr') cellstr('Cwpr') cellstr('Ws')  cellstr('Ws_bgrd_burst')  cellstr('Ws_all')...
    cellstr('uw2') cellstr('stress_RE') cellstr('TKE') cellstr('stress_tke') cellstr('n')];

    
hdr2_data=[cellstr('') cellstr('km') cellstr('dbar') cellstr('cm/s') cellstr('cm/s') cellstr('cm/s') cellstr('cm/s') cellstr('cm/s') cellstr('cm/s')...
          cellstr('count') cellstr('count') cellstr('count') cellstr('%') cellstr('%') cellstr('%') cellstr('cm2/s2') cellstr('cm2/s2') cellstr('cm2/s2') cellstr('cm2/s2')...
          cellstr('cm2/s2') cellstr('cm2/s2') cellstr('degree') cellstr('degree') cellstr('degree') cellstr('degC') cellstr('heading')...
          cellstr('') cellstr('') cellstr('mg/L') cellstr('mg/L') cellstr('mg/L') cellstr('') cellstr('') cellstr('') cellstr('mm/s')...
          cellstr('mm/s') cellstr('mm/s') cellstr('cm2/s2') cellstr('Pa') cellstr('cm2/s2') cellstr('Pa') cellstr('')];
      
hdr3_data=[cellstr('') cellstr('') cellstr('') cellstr('') cellstr('') cellstr('') cellstr('') cellstr('') cellstr('')... 
    cellstr('') cellstr('') cellstr('') cellstr('') cellstr('') cellstr('') cellstr('') cellstr('') cellstr('') cellstr('') cellstr('') cellstr('')...
    cellstr('') cellstr('') cellstr('') cellstr('') cellstr('')...
    cellstr('') cellstr('')  cellstr('') cellstr('') cellstr('')...
    cellstr('') cellstr('') cellstr('') cellstr('')  cellstr('')  cellstr('')...
    cellstr('') cellstr('') cellstr('') cellstr('') cellstr('')];
      
Navg=[id dist NORTEK(:,14) NORTEK(:,39) NORTEK(:,1) NORTEK(:,2)  NORTEK(:,3)  NORTEK(:,29) NORTEK(:,30) NORTEK(:,31) NORTEK(:,4:9) NORTEK(:,16:21) NORTEK(:,10:13) NORTEK(:,15) NORTEK(:,26:27) NORTEK(:,22) C_pump NORTEK(:,37) NORTEK(:,23:25) NORTEK(:,32) NORTEK(:,40) NORTEK(:,38) NORTEK(:,33:36) NORTEK(:,28)];
Navg2=[id dist NORTEK(:,14) NORTEK(:,1) NORTEK(:,2)  NORTEK(:,3)  NORTEK(:,29) NORTEK(:,30) NORTEK(:,31) NORTEK(:,4:9) NORTEK(:,16:21) NORTEK(:,10:13) NORTEK(:,15) NORTEK(:,26:27) NORTEK(:,22) C_pump NORTEK(:,37) NORTEK(:,23:25) NORTEK(:,32) NORTEK(:,40) NORTEK(:,38) NORTEK(:,33:36) NORTEK(:,28)];
%% Write to CSV file
    fid = fopen([folder,cruise,'_NORTEK.csv'], 'w') ;
    fprintf(fid,'%s,',hdr1_data{1:end-1});
    fprintf(fid,'%s\n',hdr1_data{end});
    fprintf(fid,'%s,',hdr2_data{1:end-1});
    fprintf(fid,'%s\n',hdr2_data{end});
    fprintf(fid,'%s,',hdr3_data{1:end-1});
    fprintf(fid,'%s\n',hdr3_data{end});
    for ii=1:size(Navg,1)
    fprintf(fid, '%f,',Navg2(ii,1));
    fprintf(fid, '%.3f,',Navg2(ii,2:end-1));
    fprintf(fid, '%3f\n',Navg2(ii,end));
    end
    fclose(fid);

%% Create and save Data Struct for Cruise
Ndata.cruise=cruise;
Ndata.id=Navg(:,1);                                             %CastID
Ndata.dist=Navg(:,2);                                           %Distance from the mouth [km]
Ndata.depth=Navg(:,3);                                          %Uncalibration Depth Nortek pressure sensor [m]
Ndata.MeanVelocity=Navg(:,4);                                   %Burst averaged Mean Velocity [cm/s]
Ndata.u=[Navg(:,5) Navg(:,8)];                                  %Burst averaged u-velocity (column 1) and standard deviation (column 2) [cm/s]
Ndata.v=[Navg(:,7) Navg(:,9)];                                  %Burst averaged u-velocity (column 1) and standard deviation (column 2) [cm/s]
Ndata.w=[Navg(:,9) Navg(:,10)];                                 %Burst averaged u-velocity (column 1) and standard deviation (column 2) [cm/s]
Ndata.backscatter=[Navg(:,11) Navg(:,12) Navg(:,13)];           %Burst averaged backscatter amplitude for beam x (column 1), y (column 2), and z (column 3) [counts]
Ndata.percentgood=[Navg(:,14) Navg(:,15) Navg(:,16)];           %Percent good for beam x (column 1), y (column 2), and z (column 3) [%]
Ndata.upr2=Navg(:,17);                                          % u'u' [cm2/s2]
Ndata.uvpr=Navg(:,18);                                          % u'v' [cm2/s2]
Ndata.uwpr=Navg(:,19);                                          % u'w' [cm2/s2]
Ndata.vpr2=Navg(:,20);                                          % v'v' [cm2/s2]
Ndata.vwpr=Navg(:,21);                                          % v'w' [cm2/s2]
Ndata.wpr2=Navg(:,22);                                          % w'w' [cm2/s2]
Ndata.heading=Navg(:,23);                                       %heading [degree]
Ndata.pitch=Navg(:,24);                                         %pitch [degree]
Ndata.roll=Navg(:,25);                                          %roll [degree]
Ndata.temperature=Navg(:,26);                                   %temperature [deg C]                         
Ndata.compass=Navg(:,27);                                       %compass heading [heading]
Ndata.b=[Navg(:,28) Navg(:,29)];                                %Regression constants from NORTEK curve used to convert backscatter to TSS  (KAF March 2014)
Ndata.C=Navg(:,30);                                             %Suspended Sediment Concentration [mg/l]
Ndata.Cmin_pump=Navg(:,31);                                     %Minimum C of pump samples [mg/L]
Ndata.Cmin_burst=Navg(:,32);                                    %Minimum C of burst [mg/L]
Ndata.Cupr=Navg(:,33);                                          %C'u'
Ndata.Cvpr=Navg(:,34);                                          %C'v'
Ndata.Cwpr=Navg(:,35);                                          %C'w'
Ndata.Ws=Navg(:,36);                                            %Settling Velocity removing background concentration determined by pump samples (Cmin_pump) [mm/s]
Ndata.Ws_bgrd_burst=Navg(:,37);                                 %Settling Velocity removing background concentration determined by burst (Cmin_burst) [mm/s]
Ndata.Ws_all=Navg(:,38);                                        %Settling Velocity with no background concentration removed [mm/s]
Ndata.uw2=Navg(:,39);                                           %Reynolds Stress,-u'w' (i.e. Kim et al, 1999) [cm2/s2]
Ndata.stress_RE=Navg(:,40);                                     %Bed stress using covaraince method (COV method, Kim et al., 1999) [Pa]
Ndata.TKE=Navg(:,41);                                           %Turbulent Kinetic Energy, E=(u'^2+v'^2+w'^2)/2 [cm2/s2]
Ndata.stress_tke=Navg(:,42);                                    %Bed stress using TKE method (Kim et al., 1999) [Pa]
Ndata.n=Navg(:,43);                                             %number of samples averaged
%%
